package com.hongbo.echartsjavawebdemo.config;

import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.SessionCookieConfig;
import javax.servlet.SessionTrackingMode;
import java.util.Collections;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    /**
     * remove jsessionid from url on startup
     * @return
     */
    @Bean
    public ServletContextInitializer servletContextInitializer() {
        return servletContext -> {
            servletContext.setSessionTrackingModes(Collections.singleton(SessionTrackingMode.COOKIE));
            SessionCookieConfig sessionCookieConfig=servletContext.getSessionCookieConfig();
            sessionCookieConfig.setHttpOnly(true);
        };
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //spring boot中可以设置默认首页，当输入域名是可以自动跳转到默认指定的网页
        //forward与redirect功能上等价，只是forward不会改变浏览器地址，redirect会改变
        registry.addViewController("/").setViewName("forward:/index");
        registry.addViewController("/index").setViewName("index");
        registry.addViewController("/main").setViewName("main");

        registry.addViewController("/echarts/staticdemo/BarDemo").setViewName("/echarts/staticdemo/BarDemo");
        registry.addViewController("/echarts/staticdemo/BarLinePieDemo").setViewName("/echarts/staticdemo/BarLinePieDemo");
        registry.addViewController("/echarts/staticdemo/BarPieDemo").setViewName("/echarts/staticdemo/BarPieDemo");
        registry.addViewController("/echarts/staticdemo/PieDemo").setViewName("/echarts/staticdemo/PieDemo");
        registry.addViewController("/echarts/staticdemo/SimpleBarDemo").setViewName("/echarts/staticdemo/SimpleBarDemo");
        registry.addViewController("/echarts/staticdemo/SimpleLineDemo").setViewName("/echarts/staticdemo/SimpleLineDemo");
        registry.addViewController("/echarts/staticdemo/SimplePieDemo").setViewName("/echarts/staticdemo/SimplePieDemo");
        registry.addViewController("/echarts/staticdemo/SimpleRadarDemo").setViewName("/echarts/staticdemo/SimpleRadarDemo");

        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);

    }
}
